package tools;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class DownloadPage {
	public static String getContentFormUrl(String url){
		HttpClient client = new DefaultHttpClient();
		HttpGet gethttp = new HttpGet(url);
		String content = null;
		HttpResponse response;
		try{
			response = client.execute(gethttp);
			HttpEntity entity = response.getEntity();
			if (entity != null){
				content = EntityUtils.toString(entity); 
				if (FunctionUtils.isCreateFile(url)  && FunctionUtils.isHasGoalContent(content) !=  -1) { 
					FunctionUtils.createFile(FunctionUtils.getGoalContent(content), url);
				}
			}
		}catch(ClientProtocolException  e){
			e.printStackTrace();
		}catch(IOException  e){
			e.printStackTrace();
		}finally{
			client.getConnectionManager().shutdown();
		}
		return content;
	}
	
	public static void main(String[] args) {
		System.out.println(DownloadPage.getContentFormUrl("http://www.baidu.com"));
	}
}
